//
//  ChatBaTableViewCell.m
//  聊吧
//
//  Created by 刘洋 on 16/6/13.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "ChatBaTableViewCell.h"

@interface ChatBaTableViewCell ()

@property(nonatomic,retain)BaseImageView *iconImage;
@property(nonatomic,retain)BaseLabel *chatterLabel;
@property(nonatomic,retain)BaseLabel *displayNameLabel;
@property(nonatomic,retain)BaseLabel *unreadLabel;
@property(nonatomic,retain)UIView *lineView;
@property(nonatomic,retain)BaseLabel *timeLabel;


@end




@implementation ChatBaTableViewCell

-(void)dealloc{
    [_timeLabel release];
    [_iconImage release];
    [_chatterLabel release];
    [_displayNameLabel release];
    [_lineView release];
    [super dealloc];
  
}




-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    
    return self;
    
    
}


-(void)createSubviews{
    
    self.iconImage = [[BaseImageView alloc]init];
    self.iconImage.image = [UIImage imageNamed:@"head"];
    self.iconImage.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.iconImage];
    [_iconImage release];
    
    self.chatterLabel = [[BaseLabel alloc]init];
    self.chatterLabel.backgroundColor  = [UIColor clearColor];
    self.chatterLabel.font = [UIFont systemFontOfSize:20];
    [self.contentView addSubview:self.chatterLabel];
    [_chatterLabel release];
    
    self.displayNameLabel = [[BaseLabel alloc]init];
    self.displayNameLabel.backgroundColor = [UIColor clearColor];
    self.displayNameLabel.textColor = [UIColor grayColor];
    self.displayNameLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.displayNameLabel];
    [_displayNameLabel release];
    
    
    self.unreadLabel = [[BaseLabel alloc]init];
    self.unreadLabel.backgroundColor = [UIColor redColor];
    self.unreadLabel.font = [UIFont systemFontOfSize:10];
    self.unreadLabel.textColor = [UIColor whiteColor];
    self.unreadLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.unreadLabel];
    [_unreadLabel release];
    
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = RGBAColor(220, 220, 220);
    [self.contentView addSubview:self.lineView];
    [_lineView release];
    
    self.timeLabel = [[BaseLabel alloc]init];
    self.timeLabel.backgroundColor =[UIColor whiteColor];
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.timeLabel];
    [_timeLabel release];
    
    
}


-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.iconImage.frame = CGRectMake(10, 5, 50, 50);
    
    self.chatterLabel.frame = CGRectMake(75, 5, ROOTWIDTH - 130, 25);
    
    self.displayNameLabel.frame = CGRectMake(75, 35, ROOTWIDTH - 130, 20);
    
   // self.timeLabel.frame = CGRectMake(ROOTWIDTH - 150, 5, 150, 20);
    
    
    self.unreadLabel.frame = CGRectMake(50, 2, 20, 20);
    self.unreadLabel.layer.masksToBounds = YES;
    self.unreadLabel.layer.cornerRadius = 10;
    
    self.lineView.frame = CGRectMake(10, 60, ROOTWIDTH - 20, 1);
    
    
   
}


-(void)setConversation:(EMConversation *)conversation{
    if (_conversation != conversation) {
        [_conversation release];
        _conversation = [conversation retain];
    }
    
    
    self.timeLabel.text = [LYSupportTools messageTimeWithTimestamp:conversation.latestMessage.timestamp];
    
    if ([self.timeLabel.text isEqualToString:@"1970-01-01 08:00"]) {
        self.timeLabel.hidden = YES;
    }else{
        self.timeLabel.hidden = NO;
    }
    
    
    
    
    [self.timeLabel sizeToFit];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo(5);
    }];
    
    
    
    self.chatterLabel.text = _conversation.chatter;

    if (_conversation.unreadMessagesCount == 0) {
        
    
        
        self.unreadLabel.hidden = YES;
        
    }else{
        self.unreadLabel.hidden = NO;
        self.unreadLabel.text = [NSString stringWithFormat:@"%ld",_conversation.unreadMessagesCount];
    }
    
    EMMessage *message = _conversation.latestMessage;
    
    id bodyClass = message.messageBodies[0];
    if ([bodyClass isKindOfClass:[EMTextMessageBody class]]) {
        EMTextMessageBody *bodyText = bodyClass;
        self.displayNameLabel.text = bodyText.text;
    }else if ([bodyClass isKindOfClass:[EMVoiceMessageBody class]]){
        EMVoiceMessageBody *bodyVocie = bodyClass;
        self.displayNameLabel.text = bodyVocie.displayName;
        
    }else if ([bodyClass isKindOfClass:[EMVideoMessageBody class]]){
        
        EMVideoMessageBody *bodyVideo = bodyClass;
        self.displayNameLabel.text = bodyVideo.displayName;
        
        
    }else if ([bodyClass isKindOfClass:[EMImageMessageBody class]]){
        
        EMImageMessageBody *bodyImage = bodyClass;
        self.displayNameLabel.text = bodyImage.displayName;
    }
    
    
    
}

@end
